package com.cff.mina.skt;

import java.net.InetSocketAddress;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.cff.pos.PosClient;

public class MinaClient extends IoHandlerAdapter {
	//要发送给服务端的消息  
	private byte[] msg; 
  
    public MinaClient(byte[] msg){  
        this.msg = msg ;  
    }  
  
    //当与服务端链接成功时Session会被创建，同时会触发该方法  
    public void sessionOpened(IoSession session) throws Exception {  
        //发送消息给服务端  
    	byte newmsg[] = new byte[msg.length+1];
    	System.arraycopy(msg,0,newmsg,0,msg.length);
    	//newmsg[msg.length] = '/r/n';
        session.write(msg) ; 
        System.out.println("发送消息---");
        PosClient.printBtye(msg);
    }  
  
    //当接收到服务端发送来的消息时，会触发该方法  
    @Override  
    public void messageReceived(IoSession session, Object message)  
    throws Exception {  
    	byte[] data = (byte[]) msg;
        System.out.println("收到服务端回复，内容：");  
        PosClient.printBtye(data);
        session.close(false) ;  
    }  
  
    @Override  
    public void exceptionCaught(IoSession session, Throwable cause)  
    throws Exception {  
        session.close(false) ;  //当发送异常，就关闭session  
    } 
}
